import * as config from './config'
import utils from './utils';

/**
 * 请求封装
 */
	
export default {
    request(param) {
        return new Promise((resolve, reject) => {
            utils.debugFn(() => {
                if (!param.url) {
                    utils.toast('异常：请求url为空')
                    console.error('异常：请求url为空')
                    debugger
                }
            })

            const urlData = []
            let header = {},
                data = {}

            if (param.header) header = Object.assign({}, header, param.header);
            if (param.data) data = Object.assign({}, data, param.data);
            if(!param.noLoading) utils.showLoading()

            param.method = param.method || 'get'
            if ((param.method.toLocaleLowerCase() === 'get' || param.method.toLocaleLowerCase() === 'delete') && data &&
                typeof data === 'object') {
                for (let i in data) {
                    urlData.push(`${i}=${data[i]}`)
                }
                param.url += urlData.length > 0 ? (param.url.indexOf('?') > -1 ? '&' : '?') + urlData.join('&') : ''
                data = {}
            }

            if(param.method === 'post' || param.method === 'put') {
                if(param.raw) {
                    header['Content-Type'] = 'application/json;charset=UTF-8'
                }
            }
            const url = `${config.HTTP_URL}${param.url}`

            uni.request({
                url,
                data,
                header,
                method: param.method,
                success: res => {
                    if (param.noCheck) {
                        resolve(res.data)
                    } else {
                        this.checkCode(res, resolve, reject)
                    }
                    if(!param.noLoading) utils.hideLoading()
                },
                fail(err) {
                    utils.toast('网络异常')
                    typeof param.fail === 'function' && param.fail(err)
                    if(!param.noLoading) utils.hideLoading()
                }
            })

        })
    },

    checkCode(res, resolve, reject) {
        if(typeof res.data.code === "number") {
            switch (res.data.code){
                case 200:
                    resolve(res.data)
                    break;
                case 500:
                    reject(res.data)
                    setTimeout(v =>{
                        utils.toast(res.data.msg || '服务器异常')
                    }, 500)
            }
        }else if(typeof res.data.status === "number") {
            switch (res.data.status) {
                case 0:
                    resolve(res.data)
                    break;
                case 400:
                    reject(res.data)
                    utils.toast(res.data.message)
                    break;
                default:
                    reject(res.data)
                    utils.toast(res.data.msg || '操作错误')
            }
        }else{
            utils.toast('服务器异常，未知状态')
            reject(res.data)
        }
    },

    //get方法
    get: function (param) {
        return this.request({
            ...param,
            method: 'get'
        })
    },
    //post方法
    post: function (param) {
        return this.request({
            ...param,
            method: 'post'
        })
    },
    //put方法
    put: function (param) {
        return this.request({
            ...param,
            method: 'PUT'
        });
    },
    //delete方法
    delete: function (param) {
        return this.request({
            ...param,
            method: 'DELETE'
        });
    },

}